package org.tmsps.ne4spring.utils.group;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.tmsps.ne4spring.utils.ChkUtil;

/**
 * 聚合工具类
 * 
 * @author mac
 *
 */
public class GroupUtil {

	/**
	 * 根据key聚合查找多个属性
	 * 
	 * @param seekersSocialBasedList
	 * @param key
	 * @return
	 */
	public static Set<String> groupBy(List<Map<String, Object>> srcList, String key1, String key2) {
		Set<String> set = new HashSet<>();
		if (ChkUtil.isNull(srcList)) {
			return set;
		}
		for (Map<String, Object> map : srcList) {
			Object val1 = map.get(key1);
			Object val2 = map.get(key2);
			if (val1 != null) {
				set.add(val1.toString() + "_" + ChkUtil.getString(val2, ""));
			}
		}
		return set;
	}

	/**
	 * 根据key聚合查找
	 * 
	 * @param seekersSocialBasedList
	 * @param key
	 * @return
	 */
	public static Set<String> groupBy(List<Map<String, Object>> srcList, String key) {
		Set<String> set = new HashSet<>();
		if (ChkUtil.isNull(srcList)) {
			return set;
		}
		for (Map<String, Object> map : srcList) {
			Object val = map.get(key);
			if (val != null) {
				set.add(val.toString());
			}
		}
		return set;
	}

	/**
	 * 逗号分割,最后转字符串
	 * 
	 * @param seekersSocialBasedList
	 * @param key
	 * @return
	 */
	public static String groupByToComma(List<Map<String, Object>> srcList, String key) {
		Set<String> valList = groupBy(srcList, key);
		String end = "";
		for (String val : valList) {
			end += val + ",";
		}
		if (end.length() > 0) {
			end.substring(0, end.length() - 1);
		}
		return end;
	}

}
